<?php

App::uses('AppController', 'Controller');

/**
 * Contents Controller
 *
 * @property Content $Content
 */
class ContentsController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->Content->locale = $this->Session->read('Language.code');
        $this->Content->recursive = 0;
        $this->set('contents', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($slug = null) {
        $this->Content->locale = $this->Session->read('Language.code');
        if (!$slug) {
            $this->Session->setFlash('Invalid id for Post.');
            $this->redirect('/posts/');
        }
        $this->set('content', $this->Content->findByPath($slug));

//		if (!$this->Content->exists($id)) {
//			throw new NotFoundException(__('Invalid content'));
//		}
//		$options = array('conditions' => array('Content.' . $this->Content->primaryKey => $id));
//		$this->set('content', $this->Content->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        $this->Content->locale = $this->Session->read('Language.code');
        $this->loadModel('Language');
        if ($this->request->is('post')) {
            $this->Content->create();
            $this->Content->locale = $this->request->data['Content']['Language'];
            if ($this->Content->save($this->request->data)) {
                $this->Session->setFlash('The new content has been saved','default',array('class'=>'alert alert-info'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('The new content could not be saved. Please, try again.','default',array('class'=>'alert alert-danger'));
            }
        }
        $language = $this->Language->find('list', array('fields' => array('Language.code', 'Language.name')));
        $this->set(compact('language'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        $this->Content->locale = $this->Session->read('Language.code');
        $this->loadModel('Language');
        if (!$this->Content->exists($id)) {
            throw new NotFoundException('Invalid content','default',array('class'=>'alert alert-danger'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            $this->Content->locale = $this->request->data['Content']['Language'];
            if ($this->Content->save($this->request->data)) {
                $this->Session->setFlash('The content has been successfully edited','default',array('class'=>'alert alert-info'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('The content could not be edited. Please, try again.','default',array('class'=>'alert alert-danger'));
            }
        } else {
            $options = array('conditions' => array('Content.' . $this->Content->primaryKey => $id));
            $this->request->data = $this->Content->find('first', $options);
        }
        $language = $this->Language->find('list', array('fields' => array('Language.code', 'Language.name')));
        $this->set(compact('language'));
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Content->id = $id;
        if (!$this->Content->exists()) {
            throw new NotFoundException('Invalid content','default',array('class'=>'alert alert-danger'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Content->delete()) {
            $this->Session->setFlash('Content deleted','default',array('class'=>'alert alert-info'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash('Content was not deleted','default',array('class'=>'alert alert-danger'));
        $this->redirect(array('action' => 'index'));
    }

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('view');
    }

}
